package net.cctms.zhishu.webAppFramework.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 网络操作相关工具类
 * Created by xuxd
 */
public class NetUtil {

	public static  NetworkInfo getActiveNetInfo(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		return connectivityManager.getActiveNetworkInfo();
	}
	
	public static boolean isNetworkAvailable(Context context) {
		NetworkInfo networkInfo = getActiveNetInfo(context);
		return  (networkInfo != null && networkInfo.isAvailable());
	}
	
	public static  boolean isNetworkConnected(Context context) {
		NetworkInfo networkInfo = getActiveNetInfo(context);
		//有的时候网络有链接，但是不可用。
		return (networkInfo != null && networkInfo.isConnected() );
	}
	
	public static  boolean isConnectedOrConnecting(Context context) {
		NetworkInfo networkInfo = getActiveNetInfo(context);
		return (networkInfo != null && networkInfo.isConnectedOrConnecting());
	}

	public static String getNetworkType(Context context){
		NetworkInfo networkInfo = getActiveNetInfo(context);
		if (networkInfo == null || !networkInfo.isConnectedOrConnecting())return "";
		if("WIFI".equalsIgnoreCase(networkInfo.getTypeName())){
			return networkInfo.getTypeName();
		}
		return networkInfo.getSubtypeName();
	}
	
	public static  boolean isConnect2G(Context context){
		
		NetworkInfo networkInfo = getActiveNetInfo(context);
		
		if (networkInfo == null || !networkInfo.isConnectedOrConnecting())return false;

		int type = networkInfo.getType();
		int subType = networkInfo.getSubtype();
		 if(type==ConnectivityManager.TYPE_MOBILE){
			 switch(subType){  
			 case TelephonyManager.NETWORK_TYPE_GPRS:  
	                return true; // ~ 100 kbps  
	         case TelephonyManager.NETWORK_TYPE_CDMA:  
	        	 return true; // ~ 14-64 kbps 
	         case TelephonyManager.NETWORK_TYPE_1xRTT:  
	                    return true;  // ~ 50-100 kbps  
	         case TelephonyManager.NETWORK_TYPE_EDGE:  
	                    return true; // ~ 50-100 kbps  
	         default:
	                	return false;
			 }
		 }else{
			 return false;
		 }
		
	}
	
	/** 
	 * make true current connect service is wifi 
	 * @param mContext 
	 * @return 
	 */  
	public static boolean isWifi(Context mContext) {
	    ConnectivityManager connectivityManager = (ConnectivityManager) mContext
	            .getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
	}

	/**
	 * 获取客户端IP地址
	 * @return
	 */
	public static String getIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr
							.nextElement();
					if (!inetAddress.isLoopbackAddress()) {

						return inetAddress.getHostAddress();
					}
				}
			}
		} catch (SocketException ex) {
			ex.printStackTrace();
		}
		return null;
	}

}
